﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Fdb.Model;
using Microsoft.AspNetCore.Mvc;
using Model;

namespace fyyui.controller
{
    [Route("api/[controller]")]
    [ApiController]
    public class CustController : ControllerBase
    {
        private readonly Test_DbContext _test_DbContext;
        public CustController(Test_DbContext test_DbContext)
        {
            _test_DbContext = test_DbContext;
        }

        [HttpPost]
        [Route("GetCustList")]
        public IActionResult GetCustList([FromBody] dynamic data)
        {
            int pageIndex = data.pageindex==null?1 : data.pageindex;
            string name = data.name == null ? string.Empty : data.name;

            Cust cust = new Cust();
            cust.name = "%"+name+"%";
            PagedQuery pagedQuery = new PagedQuery();
            pagedQuery.IdField = "ID";
            pagedQuery.PageIndex = pageIndex;
            pagedQuery.SortClause = "ID asc";
            pagedQuery.PageSize = 10;
            List<Cust> custList = _test_DbContext.Cust.FetchPageList(cust, pagedQuery);
            var result = new { success = true,message="查询成功！",data = custList, total = _test_DbContext.Cust.FetchCount(pagedQuery), pageSize = pagedQuery.PageSize };
            return Ok(result);
        }

        [HttpPost]
        [Route("GetCust")]
        public IActionResult GetCust([FromBody] dynamic data)
        {
            int _id = (data.id == null || data.id == "") ? -1 : data.id;
            if (_id <= 0)
            {
                return Ok(new { success = false, message = "未找到记录！" });
            }

            Cust cust = _test_DbContext.Cust.Fetch(_id);
            if (cust == null)
            {
                return Ok(new { success = false, message = "未找到记录！" });
            }

            return Ok(new { success = true, message = "查询成功！", data = cust });
        }

        [HttpPost]
        [Route("AddCust")]
        public IActionResult AddCust([FromBody] dynamic data)
        {
            int _id = (data.id == null || data.id=="") ? -1 : data.id;
            string _name = data.name == null ? "" : data.name;
            int _edu = (data.edu == null || data.edu == "") ? 0 : data.edu;
            int _major = (data.major == null || data.major == "") ? 0 : data.major;

            Cust cust = new Cust();
            if (_id > 0)
            {
                cust = _test_DbContext.Cust.Fetch(_id);
                if (cust == null)
                {
                    return Ok(new { success = false, message = "未找到记录！" });
                }
            }

            cust.name = _name;
            cust.edu = _edu;
            cust.major = _major;
            ExecuteResult er = new ExecuteResult();
            if (cust.id > 0)
            {
                er = _test_DbContext.Cust.Update(cust);
            }
            else
            {
                er = _test_DbContext.Cust.Insert(cust);
            }

            return Ok(new { success = er.Success, message = er.ResultString });
        }

        [HttpPost]
        [Route("DelCust")]
        public IActionResult DelCust([FromBody] dynamic data)
        {
            int _id = data.id == null ? -1 : data.id;//int不能用data.id==""进行比对

            Cust cust = new Cust();
            if (_id <= 0)
            {
                return Ok(new { success = false, message = "未找到记录！" });
            }

            ExecuteResult er = er = _test_DbContext.Cust.Delete(_id);
            return Ok(new { success = er.Success, message = er.ResultString });
        }
    }
}